## tidyverse 1.3.0 for data manipulation and plotting
if (!require(tidyverse)) install.packages('tidyverse')
library(tidyverse)

## rnaturalearth 0.1.0 for building world map
if (!require(rnaturalearth)) install.packages('rnaturalearth')
library(rnaturalearth)

## rnaturalearthdata 0.1.0 for building world map
if (!require(rnaturalearthdata)) install.packages('rnaturalearthdata')
library(rnaturalearthdata)

## sf 0.9-3 for building world map
if (!require(sf)) install.packages('sf')
library(sf)

## rgeos 0.5-3 for building world map
if (!require(rgeos)) install.packages('rgeos')
library(rgeos)

## gganimate 1.0.5 for animating plots
if (!require(gganimate)) install.packages('gganimate')
library(gganimate)
volcano <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/volcano.csv")

eruptions <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/eruptions.csv")

events <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/events.csv")

tree_rings <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/tree_rings.csv")

sulfur <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-12/sulfur.csv")

world <- ne_countries(scale = "medium", returnclass = "sf")
## remove NA/unknown years from eruption and volcano datasets
eruptions_clean <- eruptions %>%
  filter(eruption_category == "Confirmed Eruption",
         !is.na(start_year))

volcano_clean <- volcano %>%
  filter(last_eruption_year != "Unknown") %>%
  select(-last_eruption_year)

## join eruption and volcano datasets
joined_volcano_eruptions <- left_join(eruptions_clean, volcano_clean) %>%
  filter(start_year >= 1600,
         !is.na(population_within_30_km),
         !is.na(vei))

## fill in years with no data
joined_volcano_eruptions <- joined_volcano_eruptions %>%
  expand(start_year = 1600:2020) %>%
  left_join(joined_volcano_eruptions)

## plot
eruptions_map <- ggplot(data = joined_volcano_eruptions) + 
  geom_sf(data = world) + 
  coord_sf(expand = FALSE) +
  theme_bw() + 
  geom_point(aes(x = longitude, y = latitude, group = start_year,
                 size = population_within_30_km, color = vei),
             shape = 17) +
  scale_color_gradient(low = "#f5af19", high = "#f12711") +
  transition_time(start_year) +
  enter_grow() +
  shadow_mark(alpha = 0.2) +
  labs(x = "Longitude",
       y = "Latitude",
       size = "Population within 100 km",
       color = "Volcanic explosivity index",
       title = "Cumulative volcanic eruptions from 1600 to the present",
       subtitle = "Year: {round(frame_time, 0)}",
       caption = "Source: @R4DScommunity, Nature | Chart: @poppn13")

## store as animated object
animated_eruptions <- animate(eruptions_map, fps = 10, nframes = 10 * 30,
                              start_pause = 10, end_pause = 25, width = 800, height = 500)
  
## save .gif
anim_save("volcanic_eruptions.gif", animated_eruptions)

## print
animated_eruptions